/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.validate.AddGroup;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.IsfdDatabaseModelTreeBo;
import com.comac.ins.isfd.domain.vo.IsfdDatabaseModelTreeShowVo;
import com.comac.ins.isfd.service.IIsfdDatabaseModelTreeService;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 要素库模型层级管理
 *
 * @author Lion Li
 * @date 2025-01-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/databaseModel/tree")
public class IsfdDatabaseModelTreeController extends BaseController {

    @Autowired
    private IIsfdDatabaseModelTreeService isfdDatabaseModelTreeService;

    /**
     * 获取要素库树结构
     */
    @SaCheckPermission("isfd:databaseModel:tree:getTree")
    @GetMapping("/getTree")
    public R<IsfdDatabaseModelTreeShowVo> tree(@RequestParam(value = "rootModelOnly", defaultValue = "0") int rootModelOnly) {
        return R.ok(isfdDatabaseModelTreeService.getTree(rootModelOnly));
    }

    /**
     * 根据业务信息查询模型树
     */
    @SaCheckPermission("isfd:databaseModel:tree:getTree")
    @GetMapping("/getTreeByBusinessId")
    public R<IsfdDatabaseModelTreeShowVo> getTreeList(@RequestParam(value = "databaseTypeId") Long databaseTypeId,
                                                            @RequestParam(value = "sceneId", required = false) Long sceneId,
                                                            @RequestParam(value = "systemId", required = false) Long systemId,
                                                            @RequestParam(value = "rootModelOnly", defaultValue = "0") int rootModelOnly) {
        return R.ok(isfdDatabaseModelTreeService.getTreeByBusinessId(databaseTypeId, sceneId, systemId, rootModelOnly));
    }

    /**
     * 新增/修改要素库模型层级管理
     */
    @SaCheckPermission("isfd:databaseModel:tree:add")
    @Log(title = "要素库模型层级管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/save")
    public R<Void> save(@Validated({AddGroup.class}) @RequestBody IsfdDatabaseModelTreeBo bo) {
        return toAjax(isfdDatabaseModelTreeService.save(bo));
    }

    /**
     * 删除要素库模型层级管理
     *
     * @param id 主键
     */
    @SaCheckPermission("isfd:databaseModel:tree:remove")
    @Log(title = "要素库模型层级管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@NotNull(message = "主键不能为空")
                          @PathVariable Long id) {
        return toAjax(isfdDatabaseModelTreeService.deleteById(id));
    }
}
